package springsecurityrabclogin.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 用户表
 * t_user
 */
//@Data
public class TUser implements UserDetails, Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 主键，自动增长，用户ID
     */
    private Integer id;

    /**
     * 登录账号
     */
    private String loginAct;

    /**
     * 登录密码
     */
    @JsonIgnore //忽略该字段， 该字段不参与json转换
    private String loginPwd;

    /**
     * 用户姓名
     */
    private String name;

    /**
     * 用户手机
     */
    private String phone;

    /**
     * 用户邮箱
     */
    private String email;

    /**
     * 账户是否没有过期，0已过期 1正常
     */
    private Integer accountNoExpired;

    /**
     * 密码是否没有过期，0已过期 1正常
     */
    private Integer credentialsNoExpired;

    /**
     * 账号是否没有锁定，0已锁定 1正常
     */
    private Integer accountNoLocked;

    /**
     * 账号是否启用，0禁用 1启用
     */
    private Integer accountEnabled;

    /**
     * 创建时间
     */
//    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 创建人
     */
    private Integer createBy;

    /**
     * 编辑时间
     */
    private Date editTime;

    /**
     * 编辑人
     */
    private Integer editBy;

    /**
     * 最近登录时间
     */
    private Date lastLoginTime;

    public TUser() {
    }

    public TUser(Integer id, String loginAct, String loginPwd, String name, String phone, String email, Integer accountNoExpired, Integer credentialsNoExpired, Integer accountNoLocked, Integer accountEnabled, Date createTime, Integer createBy, Date editTime, Integer editBy, Date lastLoginTime) {
        this.id = id;
        this.loginAct = loginAct;
        this.loginPwd = loginPwd;
        this.name = name;
        this.phone = phone;
        this.email = email;
        this.accountNoExpired = accountNoExpired;
        this.credentialsNoExpired = credentialsNoExpired;
        this.accountNoLocked = accountNoLocked;
        this.accountEnabled = accountEnabled;
        this.createTime = createTime;
        this.createBy = createBy;
        this.editTime = editTime;
        this.editBy = editBy;
        this.lastLoginTime = lastLoginTime;
    }

    /**
     * 获取
     * @return id
     */
    public Integer getId() {
        return id;
    }

    /**
     * 设置
     * @param id
     */
    public void setId(Integer id) {
        this.id = id;
    }

    /**
     * 获取
     * @return loginAct
     */
    public String getLoginAct() {
        return loginAct;
    }

    /**
     * 设置
     * @param loginAct
     */
    public void setLoginAct(String loginAct) {
        this.loginAct = loginAct;
    }

    /**
     * 获取
     * @return loginPwd
     */
    public String getLoginPwd() {
        return loginPwd;
    }

    /**
     * 设置
     * @param loginPwd
     */
    public void setLoginPwd(String loginPwd) {
        this.loginPwd = loginPwd;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    /**
     * 获取
     * @return phone
     */
    public String getPhone() {
        return phone;
    }

    /**
     * 设置
     * @param phone
     */
    public void setPhone(String phone) {
        this.phone = phone;
    }

    /**
     * 获取
     * @return email
     */
    public String getEmail() {
        return email;
    }

    /**
     * 设置
     * @param email
     */
    public void setEmail(String email) {
        this.email = email;
    }

    /**
     * 获取
     * @return accountNoExpired
     */
    public Integer getAccountNoExpired() {
        return accountNoExpired;
    }

    /**
     * 设置
     * @param accountNoExpired
     */
    public void setAccountNoExpired(Integer accountNoExpired) {
        this.accountNoExpired = accountNoExpired;
    }

    /**
     * 获取
     * @return credentialsNoExpired
     */
    public Integer getCredentialsNoExpired() {
        return credentialsNoExpired;
    }

    /**
     * 设置
     * @param credentialsNoExpired
     */
    public void setCredentialsNoExpired(Integer credentialsNoExpired) {
        this.credentialsNoExpired = credentialsNoExpired;
    }

    /**
     * 获取
     * @return accountNoLocked
     */
    public Integer getAccountNoLocked() {
        return accountNoLocked;
    }

    /**
     * 设置
     * @param accountNoLocked
     */
    public void setAccountNoLocked(Integer accountNoLocked) {
        this.accountNoLocked = accountNoLocked;
    }

    /**
     * 获取
     * @return accountEnabled
     */
    public Integer getAccountEnabled() {
        return accountEnabled;
    }

    /**
     * 设置
     * @param accountEnabled
     */
    public void setAccountEnabled(Integer accountEnabled) {
        this.accountEnabled = accountEnabled;
    }

    /**
     * 获取
     * @return createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * 设置
     * @param createTime
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * 获取
     * @return createBy
     */
    public Integer getCreateBy() {
        return createBy;
    }

    /**
     * 设置
     * @param createBy
     */
    public void setCreateBy(Integer createBy) {
        this.createBy = createBy;
    }

    /**
     * 获取
     * @return editTime
     */
    public Date getEditTime() {
        return editTime;
    }

    /**
     * 设置
     * @param editTime
     */
    public void setEditTime(Date editTime) {
        this.editTime = editTime;
    }

    /**
     * 获取
     * @return editBy
     */
    public Integer getEditBy() {
        return editBy;
    }

    /**
     * 设置
     * @param editBy
     */
    public void setEditBy(Integer editBy) {
        this.editBy = editBy;
    }

    /**
     * 获取
     * @return lastLoginTime
     */
    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    /**
     * 设置
     * @param lastLoginTime
     */
    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public String toString() {
        return "TUser{id = " + id + ", loginAct = " + loginAct + ", loginPwd = " + loginPwd + ", name = " + name + ", phone = " + phone + ", email = " + email + ", accountNoExpired = " + accountNoExpired + ", credentialsNoExpired = " + credentialsNoExpired + ", accountNoLocked = " + accountNoLocked + ", accountEnabled = " + accountEnabled + ", createTime = " + createTime + ", createBy = " + createBy + ", editTime = " + editTime + ", editBy = " + editBy + ", lastLoginTime = " + lastLoginTime + "}";
    }


    /**
     * 用户角色List
     */
    @JsonIgnore //忽略该字段， 该字段不参与json转换
    private List<TRole> tRoleList;
    // Getter 方法：获取权限列表
    public List<TRole> getTRoleList() {
        return tRoleList;
    }

    // Setter 方法：设置权限列表
    public void setTRoleList(List<TRole> tRoleList) {
        this.tRoleList = tRoleList;
    }
    /**
     * 用户权限标识符（权限code）List
     */
    @JsonIgnore //忽略该字段， 该字段不参与json转换
    private List<TPermission> tPermissionList;

    // Getter 方法：获取权限列表
    public List<TPermission> getTPermissionList() {
        return tPermissionList;
    }

    // Setter 方法：设置权限列表
    public void setTPermissionList(List<TPermission> tPermissionList) {
        this.tPermissionList = tPermissionList;
    }

    //---------------实现UserDetails接口中的方法---------------


    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Collection<GrantedAuthority> authorities = new ArrayList<>();
//        Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
        //基于角色
//        for (TRole tRole : this.tRoleList) {
//            //放入角色
//            authorities.add(new SimpleGrantedAuthority("ROLE_" + tRole.getRole()));
//        }
//        return authorities; //可以返回用户的权限（角色、权限code）


//        基于权限(权限标识符、权限code、权限代码）
        for (TPermission tPermission : this.tPermissionList) {
            //放入权限(权限标识符、权限code、权限代码）
            authorities.add(new SimpleGrantedAuthority(tPermission.getCode()));
        }
        return authorities; //可以返回用户的权限（角色、权限code）

//        return null;

    }

    @JsonIgnore
    @Override
    public String getPassword() {
        return this.loginPwd;
    }

    @Override
    public String getUsername() {
        return this.loginAct;
    }


    @Override
    public boolean isEnabled() {
        return this.accountEnabled == 1;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return this.credentialsNoExpired == 1;
    }

    @Override
    public boolean isAccountNonLocked() {
        return this.accountNoLocked == 1;
    }

    @Override
    public boolean isAccountNonExpired() {
        return this.accountNoExpired == 1;
    }
}